<template>
    <div>
      <h2>学生信息管理</h2>
      <button class="add-button" @click="openFormDialog(false)">新增学生</button>

      <table class="student-table">
        <thead>
          <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>出生日期</th>
            <th>学院</th>
            <th>专业</th>
            <th>班级</th>
            <th>入学日期</th>
            <th>毕业日期</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="student in students" :key="student.id">
            <td>{{ student.studentId }}</td>
            <td>{{ student.name }}</td>
            <td>{{ student.birthDate }}</td>
            <td>{{ student.college }}</td>
            <td>{{ student.major }}</td>
            <td>{{ student.studentClass }}</td>
            <td>{{ student.enrollmentDate }}</td>
            <td>{{ student.graduationDate }}</td>
            <td>
              <button @click="openFormDialog(true, student)">编辑</button>
              <button @click="deleteStudent(student.id)">删除</button>
            </td>
          </tr>
        </tbody>
      </table>

      <!-- 模态框 -->
      <div v-if="showFormDialog" class="modal">
        <div class="modal-content">
          <span class="close" @click="cancelEdit">&times;</span>
          <h3>{{ isEditing ? '编辑学生信息' : '新增学生' }}</h3>
          <form @submit.prevent="submitForm">
            <label>
              学号:
              <input type="text" v-model="editingStudent.studentNumber" placeholder="学号" required>
            </label>
            <label>
              姓名:
              <input type="text" v-model="editingStudent.name" placeholder="姓名" required>
            </label>
            <label>
              出生日期:
              <input type="date" v-model="editingStudent.birthDate" required>
            </label>
            <label>
              学院:
              <input type="text" v-model="editingStudent.college" placeholder="学院" required>
            </label>
            <label>
              专业:
              <input type="text" v-model="editingStudent.major" placeholder="专业" required>
            </label>
            <label>
              班级:
              <input type="text" v-model="editingStudent.class" placeholder="班级" required>
            </label>
            <label>
              入学日期:
              <input type="date" v-model="editingStudent.enrollmentDate" required>
            </label>
            <label>
              毕业日期:
              <input type="date" v-model="editingStudent.graduationDate" required>
            </label>
            <button class="submit-button" type="submit">{{ isEditing ? '保存修改' : '添加学生' }}</button>
          </form>
        </div>
      </div>
    </div>
  </template>

<script>
import axios from 'axios'

export default {
  data () {
    return {
      students: [],
      editingStudent: null,
      isEditing: false,
      showFormDialog: false
    }
  },
  created () {
    this.fetchStudents()
  },
  methods: {
    fetchStudents () {
      axios.post('/api/querystulist', {}, {
        headers: {
          token: sessionStorage.getItem('authToken')// 添加token到请求头
        }
      }) // 替换为你的 API 端点
        .then(response => {
          this.students = response.data.data
        })
        .catch(error => {
          if (error.response && error.response.status === 401) {
            alert('Token过期,请重新登录')
            // 删除无效token
            sessionStorage.removeItem('authToken')
            // 重定向到登录页面
            this.$router.push({ name: 'login' })
          } else {
            console.error('无法获取学生数据:', error)
          }
        })
    },
    openFormDialog (editing, student = null) {
      this.isEditing = editing
      this.showFormDialog = true
      if (editing) {
        this.editingStudent = { ...student }
      } else {
        this.editingStudent = {
          studentNumber: '',
          name: '',
          birthDate: '',
          college: '',
          major: '',
          class: '',
          enrollmentDate: '',
          graduationDate: ''
        }
      }
    },
    submitForm () {
      if (this.isEditing) {
        axios.put(`/students/${this.editingStudent.id}`, this.editingStudent)
          .then(this.fetchStudents)
          .catch(error => {
            console.error('无法更新学生信息:', error)
          })
      } else {
        axios.post('/students', this.editingStudent)
          .then(this.fetchStudents)
          .catch(error => {
            console.error('无法添加学生:', error)
          })
      }
      this.cancelEdit()
    },
    cancelEdit () {
      this.showFormDialog = false
      this.editingStudent = null
    },
    deleteStudent (id) {
      axios.delete(`/students/${id}`)
        .then(this.fetchStudents)
        .catch(error => {
          console.error('无法删除学生:', error)
        })
    }
  }
}
</script>

  <style scoped>
  html, body {
    margin: 0;
    font-family: Arial, sans-serif;
    background-color: #f8f9fa;
  }
  .add-button {
    padding: 10px 15px;
    margin-bottom: 20px;
    border: none;
    background-color: #007bff;
    color: #fff;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s;
  }
  .add-button:hover {
    background-color: #0056b3;
  }
  .student-table {
    width: 100%;
    margin-bottom: 20px;
    border-collapse: collapse;
  }
  th, td {
    padding: 12px;
    border: 1px solid #dee2e6;
    text-align: left;
  }
  .modal {
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    z-index: 1000;
  }
  .modal-content {
    background-color: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    width: 500px;
    position: relative;
  }
  .close {
    position: absolute;
    top: 10px;
    right: 15px;
    cursor: pointer;
    font-size: 24px;
    color: #888;
    transition: color 0.2s;
  }
  .close:hover {
    color: #333;
  }
  form {
    display: flex;
    flex-direction: column;
    max-height: 70vh;
    overflow-y: auto;
  }
  label {
    margin-bottom: 15px;
    font-weight: bold;
  }
  input[type="text"], input[type="date"] {
    width: 100%;
    padding: 8px;
    margin-top: 5px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 14px;
  }
  .submit-button {
    padding: 10px;
    background-color: #28a745;
    color: #fff;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s;
    align-self: flex-start;
  }
  .submit-button:hover {
    background-color: #218838;
  }
  </style>
